package com.hdyg.sixg.activity.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hdyg.sixg.R;
import com.hdyg.sixg.activity.common.BaseActivity;
import com.hdyg.sixg.adapter.BuyMiniAdapter;
import com.hdyg.sixg.entry.MiniShopBean;
import com.hdyg.sixg.mvp.Contrant.CMiniShop;
import com.hdyg.sixg.mvp.base.BaseUrl;
import com.hdyg.sixg.mvp.base.GetParamUtil;
import com.hdyg.sixg.mvp.base.SpMsg;
import com.hdyg.sixg.mvp.presenter.PMiniShop;
import com.hdyg.sixg.ui.CustomLoadMoreView;
import com.hdyg.sixg.util.DataCenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.List;

import butterknife.BindView;

/**
 * @author CZB
 * @describe 矿机商城
 * @time 2019/4/17 15:19
 */
public class MiniShopActivity extends BaseActivity implements CMiniShop.IVMiniShop {

    private final static int REQUEST_CODE = 0x00001;

    @BindView(R.id.ll_top_left)
    LinearLayout llTopLeft;
    @BindView(R.id.tvTopTitle)
    TextView tvTopTitle;
    @BindView(R.id.rv_main)
    RecyclerView rvMain;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;

    private BuyMiniAdapter mAdapter;
    private CMiniShop.IPMiniShop mPresenter;
    private int page = 1, allDataSize;
    private List<MiniShopBean.ListBean> dataBeans;
    private boolean IS_SET_RESULT = false;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_recyclerview;
    }

    @Override
    protected void initView() {
        tvTopTitle.setText(R.string.main_buy_mini);

        rvMain.setLayoutManager(new LinearLayoutManager(mContext));
        onClickListener();
    }

    private void onClickListener() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            srlRefresh.finishRefresh(2000);//传入false表示刷新失败
            page = 1;
            initData();
        });
        llTopLeft.setOnClickListener(v -> {
            if (IS_SET_RESULT){
                setResult(RESULT_OK);
            }
            finish();
        });

    }

    @Override
    protected void initData() {
        mPresenter.pGetInit(BaseUrl.MINI_SHOP_URL, GetParamUtil.getMiniShopParam(page));
    }

    @Override
    protected void createPresenter() {
        mPresenter = new PMiniShop(this);
    }

    private void initAdapter() {
        mAdapter = new BuyMiniAdapter(R.layout.item_buy_mini, dataBeans);
        mAdapter.setLoadMoreView(new CustomLoadMoreView());
        rvMain.setAdapter(mAdapter);
        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            int id = view.getId();
            if (id == R.id.bt_buy) {
                mBundle = new Bundle();
                mBundle.putString(SpMsg.INTENT_STR, mAdapter.getData().get(position).getId());
                startActivityForResult(BuyMiniChildActivity.class, mBundle, REQUEST_CODE);
            }
        });
        mAdapter.setOnLoadMoreListener(() -> {
            if (mAdapter.getData().size() < allDataSize) {
                page++;
                initData();
            } else {
                mAdapter.loadMoreEnd();
            }
        }, rvMain);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            if (IS_SET_RESULT){
                setResult(RESULT_OK);
            }
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case REQUEST_CODE:
                IS_SET_RESULT = true;
                break;
        }
    }

    @Override
    public void vGetInitSuccess(MiniShopBean dataBean) {
        dataBeans = dataBean.getList();
        allDataSize = dataBean.getTotal_num();//总条数
        if (page == 1) {
            if (mAdapter != null) {
                mAdapter.setNewData(dataBeans);
            } else {
                initAdapter();
            }
        } else {
            if (dataBeans.size() != 0) {
                mAdapter.addData(dataBeans);
            }
            mAdapter.loadMoreComplete();
        }
    }
}
